package com.boco.blch.analysiser.service;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boco.blch.analysiser.compliance.ComplianceFactory;
import com.boco.blch.analysiser.compliance.ComplianceResolved;
import com.boco.blch.analysiser.exception.SerialNoNotFoundException;

public class ResultAnalysisService {
	private static Logger logger = Logger.getLogger(ResultAnalysisService.class);

	public static String service(String serialNo, String plantform, String nodeType, String result) {
		logger.info("###serialNo : " + serialNo + "###The result to analyse is : " + subString(result, 100));

		try {
			return String
					.valueOf(ComplianceFactory.getInstance(serialNo, plantform, nodeType).realEchoResolved(result));
		} catch (SerialNoNotFoundException e) {
			logger.error("没有找到针对 " + serialNo + " 的解析程序，程序将自动返回失败值！");
			return String.valueOf(ComplianceResolved.FAILED);
		} catch (NullPointerException e) {
			logger.error(serialNo + e);
			return String.valueOf(ComplianceResolved.FAILED);
		} catch (Exception e) {
			logger.error("针对 " + serialNo + " 的解析程序进行解析时出现错误(" + e + ")，程序将自动返回失败值！");
			return String.valueOf(ComplianceResolved.FAILED);
		}

	}

	private static String subString(String source, int length) {
		if (source.length() > length)
			return source.substring(0, length) + "...(由于数据达到 " + source.length() + " 位，已省略" + length + "位后的字符)";
		else
			return source;
	}
}
